<?php
/**
 * Description of fwiApplication
 *
 * @author trunghpb
 */

class fwiSimpleApplication implements infApplication {
    private static $name = null;
    private static $path = null;
    private static $baseURL = null;
    const METHOD_PREFIX = 'on';

    public function getPath($path) {
        self::$path = $path;
    }

    public static function registry($name) {
        _fwig::initialize();

        _fwia::setName($name);
        _fwia::setPath(dirname(getcwd()));
//        _fwia::setBaseURL(_fwig::$req->getServer('HTTP_HOST').'/'.$name.'/');
        _fwia::setBaseURL('http://localhost/fwi/');
//        echo _fwia::BaseURL(), '<br/>';
    }

    public static function run() {
        try {
            $controllerName = _fwig::$req->getParam('ctrl').'Controller';
            $eventName = _fwig::$req->getParam('event');
            require_once _fwia::AppPath().'/server/controller/'.$controllerName.'.php';

            $controller = new $controllerName();
            if ($eventName) {
                $methodName = self::METHOD_PREFIX.$eventName;
            }else {
                $methodName = 'onDef';
            }

            $controller->onLoad();
            $controller->runTriggerBefore($methodName);
            if (method_exists($controller, $methodName)) {
                $controller->$methodName();
            }else {
                $controller->onDef();
            }
            $controller->onLeave();
        }catch (fwiException $exception) {
            fwiExceptionHandler::handler($exception);
        }
    }
}
?>
